package com.example.choupaiapp.main.activity.publish;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.main.activity.home.CameraActivity;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.callback.ScrollChangedListener;
import com.example.choupaiapp.main.constant.Url;
import com.example.choupaiapp.main.view.MyWebview;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2020/4/21.
 */

public class PublishWebViewActivity extends CommonActivity {

    @BindView(R.id.webview)
    MyWebview webView;
    @BindView(R.id.no_tip)
    TextView no_tip;
    private String url;
    private boolean isFinish = false;
    private boolean isClick = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_webview);
    }

    @Override
    protected void initView() {
        setTitle("发布规范");

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            url = bundle.getString("url");
        }

        no_tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpParams params = new HttpParams();
                params.put("type", 2);
                finalOkGo.requestHead(RequestType.POSTHEAD, Url.CANCEL_TIP, params, Url.CANCEL_TIP, new Callback<SendCodeBean>() {
                    @Override
                    public void onSuccess(SendCodeBean sendCodeBean) {
                        finalOkGo.requestHead(RequestType.POSTHEAD, Url.PUBLISH_LOOK_SURE, null, Url.PUBLISH_LOOK_SURE, new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                            }
                        });
                        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                            @Override
                            public void onSuccess(UserInfoBean userInfoBean) {
                                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                                }
                            }
                        });
                        if (rxPermissions.isGranted(Manifest.permission.CAMERA)) {
                            if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                showTakePhoto();
                            } else {
                                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean -> {
                                    if (aBoolean) {
                                        showTakePhoto();
                                    } else {
                                        showToast("拒绝权限可能导致功能不能正常使用");
                                    }
                                });
                            }
                        } else {
                            rxPermissions.request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                                if (aBoolean) {
                                    if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                        showTakePhoto();
                                    } else {
                                        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean1 -> {
                                            if (aBoolean1) {
                                                showTakePhoto();
                                            } else {
                                                showToast("拒绝权限可能导致功能不能正常使用");
                                            }
                                        });
                                    }
                                } else {
                                    showToast("拒绝权限可能导致功能不能正常使用");
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        finalOkGo.requestHead(RequestType.POSTHEAD, Url.PUBLISH_LOOK_SURE, null, Url.PUBLISH_LOOK_SURE, new Callback<SendCodeBean>() {
                            @Override
                            public void onSuccess(SendCodeBean sendCodeBean) {
                            }
                        });
                        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                            @Override
                            public void onSuccess(UserInfoBean userInfoBean) {
                                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                                }
                            }
                        });
                        if (rxPermissions.isGranted(Manifest.permission.CAMERA)) {
                            if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                showTakePhoto();
                            } else {
                                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean -> {
                                    if (aBoolean) {
                                        showTakePhoto();
                                    } else {
                                        showToast("拒绝权限可能导致功能不能正常使用");
                                    }
                                });
                            }
                        } else {
                            rxPermissions.request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                                if (aBoolean) {
                                    if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                        showTakePhoto();
                                    } else {
                                        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean1 -> {
                                            if (aBoolean1) {
                                                showTakePhoto();
                                            } else {
                                                showToast("拒绝权限可能导致功能不能正常使用");
                                            }
                                        });
                                    }
                                } else {
                                    showToast("拒绝权限可能导致功能不能正常使用");
                                }
                            });
                        }
                    }
                });
            }
        });

        WebSettings webSetting = webView.getSettings();

        //设置自适应屏幕  缩小宽度以适合屏幕的内容
        webSetting.setLoadWithOverviewMode(true);
        //设置是否允许进行缩放
        webSetting.setTextZoom(100);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
//        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setDisplayZoomControls(true); //隐藏原生的缩放控件
        webSetting.setBlockNetworkImage(false);//解决图片不显示
        webSetting.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSetting.setDefaultTextEncodingName("utf-8");//设置编码格式

        webView.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                isFinish = true;
                setTitle(view.getTitle());
                dismissDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showDialog(PublishWebViewActivity.this, null);
                setTitle(view.getTitle());
            }
        });


        //监听网页的加载进度
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {
            }

            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
                setTitle(s);
            }
        });


        webView.setOnMyScrollChangedListener(new ScrollChangedListener() {
            @Override
            public void onChanged(int l, int t, int oldl, int oldt) {
//                if (isFinish) {
                //WebView的总高度
                float webViewContentHeight = webView.getContentHeight() * webView.getScale();
                //WebView的现高度
                float webViewCurrentHeight = (webView.getHeight() + t);
                Logger.e(t + "+++" + webView.getHeight() + "+++" + webView.getScale() + "+++" + webView.getScaleY());
                Logger.e(webViewContentHeight + "--------------" + webViewCurrentHeight);
//                if ((((int) webViewContentHeight - webViewCurrentHeight) <= 0)) {
//                    isClick = true;
//                }
                if (webViewCurrentHeight >= webViewContentHeight / 2) {
                    isClick = true;
                }
//                }
            }
        });

        webView.loadUrl(url);
    }

    @Override
    protected void initData() {

    }

    private void showTakePhoto() {
        Bundle bundle = new Bundle();
        bundle.putString("flag", "out");
        bundle.putInt("count", 9);
        startFinishActivity(CameraActivity.class, bundle);

    }

    @OnClick(R.id.mine_know)
    public void onViewClicked() {
        if (Utils.isDoubleClick()) {
            return;
        }
        if (isClick) {
            finalOkGo.requestHead(RequestType.POSTHEAD, Url.PUBLISH_LOOK_SURE, null, Url.PUBLISH_LOOK_SURE, new Callback<SendCodeBean>() {
                @Override
                public void onSuccess(SendCodeBean sendCodeBean) {
                }
            });
            finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
                @Override
                public void onSuccess(UserInfoBean userInfoBean) {
                    if (userInfoBean.getCode() == Contacts.SUCCESS) {
                        SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                    }
                }
            });
            if (rxPermissions.isGranted(Manifest.permission.CAMERA)) {
                if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    showTakePhoto();
                } else {
                    rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean -> {
                        if (aBoolean) {
                            showTakePhoto();
                        } else {
                            showToast("拒绝权限可能导致功能不能正常使用");
                        }
                    });
                }
            } else {
                rxPermissions.request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                    if (aBoolean) {
                        if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            showTakePhoto();
                        } else {
                            rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean1 -> {
                                if (aBoolean1) {
                                    showTakePhoto();
                                } else {
                                    showToast("拒绝权限可能导致功能不能正常使用");
                                }
                            });
                        }
                    } else {
                        showToast("拒绝权限可能导致功能不能正常使用");
                    }
                });
            }
        } else {
            showImageToas("请完整阅读以下规范");
        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.destroy();
        }

    }
}

